﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace MyClub.Web.UI.WebControls
{
    /// <summary>
    /// Defines the base class for state managed objects.
    /// </summary>
    public abstract class StateManagedObject: IStateManager
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StateManagedObject"/> class.
        /// </summary>
        protected StateManagedObject()
        {
        }

        private readonly StateBag viewState = new StateBag();

        private IStateManager StateManager
        {
            get { return (IStateManager)viewState; }
        }

        /// <summary>
        /// Gets the ViewState.
        /// </summary>
        /// <value>The ViewState.</value>
        protected StateBag ViewState
        {
            get { return viewState; }
        }

        /// <summary>
        /// When implemented by a class, gets a value indicating whether a server control is tracking its view state changes.
        /// </summary>
        /// <value></value>
        /// <returns>true if a server control is tracking its view state changes; otherwise, false.
        /// </returns>
        protected virtual bool IsTrackingViewState
        {
            get { return StateManager.IsTrackingViewState; }
        }

        /// <summary>
        /// When implemented by a class, loads the server control's previously saved view state to the control.
        /// </summary>
        /// <param name="state">An <see cref="T:System.Object"/> that contains the saved view state values for the control.</param>
        protected virtual void LoadViewState(object state)
        {
            StateManager.LoadViewState(state);
        }

        /// <summary>
        /// When implemented by a class, saves the changes to a server control's view state to an <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// The <see cref="T:System.Object"/> that contains the view state changes.
        /// </returns>
        protected virtual object SaveViewState()
        {
            return StateManager.SaveViewState();
        }

        /// <summary>
        /// When implemented by a class, instructs the server control to track changes to its view state.
        /// </summary>
        protected virtual void TrackViewState()
        {
            StateManager.TrackViewState();
        }

        /// <summary>
        /// When implemented by a class, gets a value indicating whether a server control is tracking its view state changes.
        /// </summary>
        /// <value></value>
        /// <returns>true if a server control is tracking its view state changes; otherwise, false.
        /// </returns>
        bool IStateManager.IsTrackingViewState
        {
            get { return IsTrackingViewState; }
        }

        /// <summary>
        /// Loads the state of the view.
        /// </summary>
        /// <param name="savedState">State of the saved.</param>
        void IStateManager.LoadViewState(object savedState)
        {
            LoadViewState(savedState);
        }

        /// <summary>
        /// When implemented by a class, saves the changes to a server control's view state to an <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// The <see cref="T:System.Object"/> that contains the view state changes.
        /// </returns>
        object IStateManager.SaveViewState()
        {
            return SaveViewState();
        }

        /// <summary>
        /// When implemented by a class, instructs the server control to track changes to its view state.
        /// </summary>
        void IStateManager.TrackViewState()
        {
            TrackViewState();
        }
    }
}
